package gdut.imis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gdut.imis.Utils.Result;
import gdut.imis.domain.College;
import gdut.imis.enums.Status;
import gdut.imis.mapper.CollegeMapper;
import gdut.imis.service.CollegeService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description
 * @Author JiaFu
 * @Date 2023/4/7 15:37
 */
@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeMapper, College> implements CollegeService {

    /***
     * 大学学院档案录入（需要由college权限）
     * @param: college
     * @return Result
     */
    @Override
    public Result addCollege(College college) {
        if (Objects.isNull(college)) {
            return Result.errorWithArgs(Status.REQUEST_PARAMS_NOT_VALID_ERROR, "college");
        }
        boolean flag = this.save(college);
        if (flag) {
            return Result.success();
        }
        return Result.errorWithArgs(Status.FAILED, "录入学院档案失败");
    }

    /***
     * 获取大学学院档案列表
     * @return Result
     */
    @Override
    public Result getCollegeList() {
        List<College> collegeList = this.list();
        if (CollectionUtils.isEmpty(collegeList)) {
            return Result.errorWithArgs(Status.FAILED, "没有查询到任何学院信息");
        }
        return Result.success(collegeList);
    }

    /***
     * 根据大学id查询学院列表
     * @param: universityId
     * @return Result
     */
    @Override
    public Result getCollegeListByUniversityId(Long universityId) {
        LambdaQueryWrapper<College> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(College::getUniversityId, universityId);
        List<College> collegeList = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(collegeList)) {
            return Result.errorWithArgs(Status.FAILED, "没有查询到任何学院信息");
        }
        return Result.success(collegeList);
    }
}
